package com.huilian.iotbox.data.service.impl;

import com.huilian.iotbox.data.dao.OrderCommonDao;
import com.huilian.iotbox.data.dto.OrderDto;
import com.huilian.iotbox.data.enums.*;
import com.huilian.iotbox.data.po.*;
import com.huilian.iotbox.data.service.*;
import com.huilian.iotbox.data.utils.Util;
import com.huilian.iotbox.data.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Slf4j
@Service
public class OrderCommonServiceImpl implements OrderCommonService {

    @Autowired
    private OrderCommonDao orderCommonDao;

    @Override
    public Long insert(
            Integer userId,
            Integer merchantStoreId,
            String deviceNo,
            Integer deviceTypeId,
            String body,
            BigDecimal money,
            int coin,
            int presentedCoin,
            Integer optionType,
            Integer mchType,
            String channelType,
            String payChannelOrderNo,
            Integer typeOfArrival,
            Integer terminalType
    ) {

        Order order = new Order();
        order.setDeviceTypeId(deviceTypeId);
        order.setOptionType(optionType);
        order.setTotalAmount(money);
        order.setSaleCoin(coin);
        order.setGiveCoin(presentedCoin);
        order.setMchType(mchType);
        order.setMerchantStoreId(merchantStoreId);
        order.setOutTradeNo(Util.getOutTradeNo());
        order.setDeviceNo(deviceNo);
        order.setBody(body);
        order.setTotalCoin(coin + presentedCoin);
        order.setRefundTotalAmount(money);
        order.setChannelType(channelType);
        order.setUserId(userId);
        order.setSettlementMoney(BigDecimal.ZERO);
        order.setStatus(OrderStatusEnum.SUCCESS.getCode());
        order.setGmtPayment(new Date());
        order.setHandlingCharge(BigDecimal.ZERO);
        order.setPayChannelOrderNo(payChannelOrderNo);
        order.setTypeOfArrival(typeOfArrival);
        order.setTerminalType(terminalType);
        orderCommonDao.insert(order);
        return order.getId();
    }

    /**
     * 根据第三方订单号和收款商户类型
     */
    @Override
    public OrderVo findOrderByPayChannelOrderNo(OrderDto orderDto) {
        OrderVo orderVo = orderCommonDao.findOrderByPayChannelOrderNo(orderDto);
        return orderVo;
    }

    /**
     * 根据第三方订单号和收款商户类型和抖音订单certificate_id
     */
    @Override
    public OrderVo findOrderByPayChannelOrderNoAndDYId(OrderDto orderDto) {
        return orderCommonDao.findOrderByPayChannelOrderNoAndDYId(orderDto);
    }

    /**
     * 增加order
     */
    @Override
    public Integer addOrder(Order order) {
        return orderCommonDao.insert(order);
    }

    /**
     * 修改订单状态
     */
    @Override
    public Integer updateStatusById(Order order) {
        Integer result = orderCommonDao.updateStatusById(order);
        return result;
    }

    @Override
    public void updateOrderTotalAmount(Order updateOrder) {
        orderCommonDao.updateOrderTotalAmount(updateOrder);
    }

    @Override
    public Integer updateHandlingCharge(Long orderId, BigDecimal handlingCharge) {
        return orderCommonDao.updateHandlingCharge(orderId, handlingCharge);
    }

    @Override
    public OrderVo findByOperationUserId(OrderDto orderDto) throws Exception {
        return orderCommonDao.findByOperationUserId(orderDto);
    }

}
